////////////////////////////////////////////////////////////////////////////
//
//  CryEngine Source File.
//  Copyright (C), Crytek.
// -------------------------------------------------------------------------
//  File name:   SaveReaderWriter_CryPak.h
//  Created:     15/02/2010 by Alex McCarthy.
//  Description: Implementation of the ISaveReader and ISaveWriter
//               interfaces using CryPak
// -------------------------------------------------------------------------
//  History:
//
////////////////////////////////////////////////////////////////////////////

#ifndef __SAVE_READER_WRITER_CRYPAK_H__
#define __SAVE_READER_WRITER_CRYPAK_H__

#include <IPlatformOS.h>

class CCryPakFile
{
protected:
	CCryPakFile(const char* fileName, const char* szMode);
	virtual ~CCryPakFile();

	IPlatformOS::EFileOperationCode CloseImpl();

	std::vector<byte> m_data;
	string m_fileName;
	FILE* m_pFile;
	size_t m_filePos;
	IPlatformOS::EFileOperationCode m_eLastError;
	bool m_bWriting;
	static const int s_dataBlockSize = 128*1024;

private:
	CDebugAllowFileAccess m_allowFileAccess;
};

class CSaveReader_CryPak : public IPlatformOS::ISaveReader, public CCryPakFile
{
public:
	CSaveReader_CryPak(const char* fileName);

	// ISaveReader
	virtual IPlatformOS::EFileOperationCode Seek(long seek, ESeekMode mode);
	virtual IPlatformOS::EFileOperationCode GetFileCursor(long& fileCursor);
	virtual IPlatformOS::EFileOperationCode ReadBytes(void* data, size_t numBytes);
	virtual IPlatformOS::EFileOperationCode GetNumBytes(size_t& numBytes);
	virtual IPlatformOS::EFileOperationCode Close() { return CloseImpl(); }
	virtual IPlatformOS::EFileOperationCode LastError() const { return m_eLastError; }
	virtual void GetMemoryUsage(ICrySizer *pSizer) const;
	virtual void TouchFile();
	//~ISaveReader
};

DECLARE_BOOST_POINTERS(CSaveReader_CryPak);

class CSaveWriter_CryPak : public IPlatformOS::ISaveWriter, public CCryPakFile
{
public:
	CSaveWriter_CryPak(const char* fileName);

	// ISaveWriter
	virtual IPlatformOS::EFileOperationCode AppendBytes(const void* data, size_t length);
	virtual IPlatformOS::EFileOperationCode Close() { return CloseImpl(); }
	virtual IPlatformOS::EFileOperationCode LastError() const { return m_eLastError; }
	virtual void GetMemoryUsage(ICrySizer *pSizer) const;
	//~ISaveWriter
};

DECLARE_BOOST_POINTERS(CSaveWriter_CryPak);

#endif //__SAVE_READER_WRITER_CRYPAK_H__
